package com.dushibao.dao;

import cn.hutool.core.util.StrUtil;
import com.dushibao.common.Constants;
import com.dushibao.entity.UploadServer;
import com.dushibao.entity.User;
import com.dushibao.utils.DBUtils;
import com.dushibao.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class UploadServerDao {

    public Page page(Page page,String name,String address) {

        try{
            StringBuilder querySql = new StringBuilder("select id,addTime,name,address,account,password,open from dushibao_upload_server where 1 = 1 ");
            if(StrUtil.isNotBlank(name)){
                querySql.append(" and name like '%"+name+"%' ");
            }
            if(StrUtil.isNotBlank(address)){
                querySql.append(" and address like '%"+address+"%' ");
            }
            querySql.append(" limit ?,? ");

            Connection conn = DBUtils.getConnection();

            PreparedStatement stmt = conn.prepareStatement(querySql.toString());
            stmt.setInt(1,page.getStart());
            stmt.setInt(2,page.getPageSize());

            ResultSet rs = stmt.executeQuery();

            List<Object> objs = new ArrayList<>();
            while(rs.next()){
                UploadServer uploadServer = new UploadServer();
                uploadServer.setId(rs.getLong("id"));
                uploadServer.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                uploadServer.setName(rs.getString("name"));
                uploadServer.setAddress(rs.getString("address"));
                uploadServer.setAccount(rs.getString("account"));
                uploadServer.setPassword(rs.getString("password"));
                uploadServer.setOpen(rs.getInt("open"));
                objs.add(uploadServer);
            }

            DBUtils.close(rs,stmt,conn);

            StringBuilder countSql = new StringBuilder("select count(*) as total from dushibao_upload_server where 1 = 1 ");
            if(StrUtil.isNotBlank(name)){
                countSql.append(" and name like '%"+name+"%' ");
            }
            if(StrUtil.isNotBlank(address)){
                countSql.append(" and address like '%"+address+"%' ");
            }

            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(countSql.toString());

            rs = stmt.executeQuery();

            int total = 0;
            if(rs.next()){
                total = rs.getInt("total");
            }

            Page iPage = new Page();
            iPage.setCurrent(page.getCurrent());
            iPage.setPageSize(page.getPageSize());
            iPage.setData(objs);
            iPage.setPages(total % iPage.getPageSize()==0?total/ iPage.getPageSize():total/ iPage.getPageSize()+1);
            iPage.setTotal(total);

            return iPage;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public UploadServer getById(long id) {
        try{
            String sql = " select id,addTime,name,address,account,password,open from dushibao_upload_server where id = ? ";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1,id);
            ResultSet rs = stmt.executeQuery();
            UploadServer obj = new UploadServer();

            if(rs.next()){
                obj.setId(rs.getLong("id"));
                obj.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                obj.setName(rs.getString("name"));
                obj.setAddress(rs.getString("address"));
                obj.setAccount(rs.getString("account"));
                obj.setPassword(rs.getString("password"));
                obj.setOpen(rs.getInt("open"));

            }

            return obj;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public Integer save(UploadServer obj){

        try{
            String sql = "insert into dushibao_upload_server(name,address,account,password,open) values(?,?,?,?,?)";

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,obj.getName());
            stmt.setString(2,obj.getAddress());
            stmt.setString(3,obj.getAccount());
            stmt.setString(4,obj.getPassword());
            stmt.setInt(5,obj.getOpen());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Integer update(UploadServer obj) {
        try{
            String sql = "update dushibao_upload_server set name=?,address=?,account=?,password=?,open=? where id=?";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,obj.getName());
            stmt.setString(2,obj.getAddress());
            stmt.setString(3,obj.getAccount());
            stmt.setString(4,obj.getPassword());
            stmt.setInt(5,obj.getOpen());
            stmt.setLong(6,obj.getId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public Integer deleteById(List<Long> ids) {
        try{

            StringBuilder sql = new StringBuilder(" delete from dushibao_upload_server where id in ( ");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));

                if(i<ids.size()-1){
                    sql.append(",");
                }

            }
            sql.append(")");

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
